﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Transport
{
    public class ThuongHieuControl
    {
        transportEntities db;

        public ThuongHieuControl()
        {
            db = new transportEntities();
        }
        public List<thuong_hieu> listAll()
        {
            try
            {
                return db.thuong_hieu.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }
        public thuong_hieu findByID(string Id)
        {
            try
            {
                return db.thuong_hieu.SingleOrDefault(t => t.ma_thuong_hieu == Id);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<thuong_hieu> findByCompanyID(string Id)
        {
            try
            {
                return db.thuong_hieu.Where(t => t.ma_hang_xe == Id).ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<thuong_hieu> findByCompanyName(string Name)
        {
            try
            {
                var result = (from a in db.thuong_hieu
                              from b in db.hang_xe
                              where a.ma_hang_xe == b.ma_hang_xe && b.ten_hang_xe == Name
                              select a);

                return result.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool insert(thuong_hieu value)
        {
            try
            {
                db.thuong_hieu.AddObject(value);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool update(thuong_hieu value)
        {
            try
            {
                thuong_hieu st = db.thuong_hieu.SingleOrDefault(t => t.ma_thuong_hieu == value.ma_thuong_hieu);
                st = value;
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool delete(string Id)
        {
            try
            {
                thuong_hieu st = db.thuong_hieu.SingleOrDefault(t => t.ma_thuong_hieu == Id);
                db.thuong_hieu.DeleteObject(st);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}